<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <!--第三方lib-->
    <title>要素预览</title>
    <script src="./lib/cesiumjs/Cesium/Cesium.js"></script>
    <link href="./lib/cesiumjs/Cesium/Widgets/Widgets.css" rel="stylesheet">
    <!--第三方lib-->
    <script include="jquery,bootstrap,bootstrap-checkbox,font-awesome,web-icons,layer,haoutil,nprogress,toastr,admui,turf,mars3d" libpath="lib/" src="lib/include-lib.js?time=20190418" type="text/javascript"></script>
    <link href="css/style.css?time=20190418" rel="stylesheet" />

    <!--      <script src="https://cesium.com/downloads/cesiumjs/releases/1.65/Build/Cesium/Cesium.js"></script>-->
    <!--  <link href="https://cesium.com/downloads/cesiumjs/releases/1.65/Build/Cesium/Widgets/widgets.css" rel="stylesheet">-->

    <style>
        html,
        body,
        #cesiumContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #000000;
        }
    </style>
</head>

<body>
    <div id="cesiumContainer" style="width: 100%"></div>
    <script>
        Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI1NjM5MjMxOS1lMWVkLTQyNDQtYTM4Yi0wZjA4ZDMxYTlmNDMiLCJpZCI6MTQ4MiwiaWF0IjoxNTI4Njc3NDQyfQ.vVoSexHMqQhKK5loNCv6gCA5d5_z3wE2M0l_rWnIP_w';
        // Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzZWE2NTRhOC1kYTZlLTQ0YzMtOWJiNi02ZTU2ZjlmOTg1MGMiLCJpZCI6MTQyODgsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1NjUxNzU4MjJ9.-MyI5VpHz6X6JXRNFDQ2OPfVQHg90qO-lrBS8PrUY5w';
        var viewer = new Cesium.Viewer('cesiumContainer', {
            terrainProvider: Cesium.createWorldTerrain(),
            animation: false, //是否显示动画控件
            homeButton: false, //是否显示home键
            geocoder: false, //是否显示地名查找控件        如果设置为true，则无法查询
            baseLayerPicker: false, //是否显示图层选择控件
            timeline: false, //是否显示时间线控件
            fullscreenButton: true, //是否全屏显示
            scene3DOnly: true, //如果设置为true，则所有几何图形以3D模式绘制以节约GPU资源
            infoBox: true, //是否显示点击要素之后显示的信息
            sceneModePicker: true, //是否显示投影方式控件  三维/二维
            navigationInstructionsInitiallyVisible: false,
            navigationHelpButton: false, //是否显示帮助信息控件
            selectionIndicator: false //是否显示指示器组件

        });
        viewer._cesiumWidget._creditContainer.style.display = "none";

        let url1 = window.location.href.split('?url=')[1];
        let dataUrl = url1.split('&type=')[0]; //通过截断的方式获得画面URL字符串中elementUrl
        // var dataUrl =  'http://data.marsgis.cn/3dtiles/qx-shuiwu/tileset.json';

        var dataType = window.location.href.split("&type=")[1]; //获得画面URL字符串中serviceType
        var serviceType = JSON.parse(dataType);

        switch (serviceType) {
            case 1:
                load3Dtiles(dataUrl);
                break;
            case 2:
                load3Dtiles(dataUrl);
                break;
            case 3:
                load3Dtiles(dataUrl);
                break;
            case 4:
                loadGeoJson(dataUrl);
        }


        function load3Dtiles(dataUrl) {
            //加载三维模型，此方法可设置相机初始视角
            var tileset = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
                url: dataUrl,
                maximumScreenSpaceError: 2,
                dynamicScreenSpaceError: true,
            }));
            tileset.modelMatrix  =  Cesium.Matrix4.fromArray([
                1.0,  0.0,  0.0,  0.0,
                0.0,  1.0,  0.0,  0.0,
                0.0,  0.0,  1.0,  0.0,
                0.0,  0.0,  0.0,  1.0
            ]);
            // viewer.scene.primitives.add(tileset);
            tileset.readyPromise.then(function(tileset) {
                viewer.scene.primitives.add(tileset);
                viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.5, -0.5, tileset.boundingSphere.radius * 0.5));
            }).otherwise(function(error) {
                console.log(111, error);
                parent.layer.alert("渲染失败，请检查服务地址是否合法", {
                    icon: 4,
                    title: "提示"
                });
            });
            // debugger
            // viewer.scene.primitives.remove(tileset);
            return tileset;
        }

        /*    function loadGeoJson(dataUrl) {
              var oneLayer;
              var kd = Cesium.GeoJsonDataSource.load(dataUrl, {
                // strokeWidth: 3,
                // clampToGround: true,
                // stroke: Cesium.Color.WHITE,
              });
              oneLayer = viewer.dataSources.add(kd).then(function (dataCtrl) {
                // data = dataCtrl;
                // data._groundPrimitives.show = false;
              });
              window.viewer.zoomTo(kd);
              window.viewer.dataSources.remove(oneLayer);
              window.viewer.dataSources.remove(kd);

            }*/

        function loadGeoJson(dataUrl) {
            var data = viewer.dataSources.add(Cesium.GeoJsonDataSource.load(dataUrl, {
                // stroke: Cesium.Color.HOTPINK,//颜色样式渲染
                // fill: Cesium.Color.PINK,
                // strokeWidth: 3,
                //markerSymbol: './gis/data/geojson/render.png'
            }));
            data.then(function(data) {
                viewer.zoomTo(data);
            }).otherwise(function(error) {
                parent.layer.alert("渲染失败，请检查服务地址是否合法", {
                    icon: 4,
                    title: "提示"
                });
            });

            // debugger
            // window.viewer.dataSources.remove(data);
        }
    </script>
</body>

</html>